package redis

import (
	"reflect"

	v1 "k8s.io/api/apps/v1"
	corev1 "k8s.io/api/core/v1"
	"k8s.io/api/policy/v1beta1"
)

// check if reconcile required

func reconcilePersistentVolumeClaimApply(foundVolume corev1.PersistentVolumeClaim, newVolume corev1.PersistentVolumeClaim) (bool, corev1.PersistentVolumeClaim) {

	reconcileRequired := false

	if !reflect.DeepEqual(foundVolume.Spec.Resources.Requests, newVolume.Spec.Resources.Requests) {
		foundVolume.Spec.Resources.Requests = newVolume.Spec.Resources.Requests
		reconcileRequired = true
	}

	return reconcileRequired, foundVolume

}

func reconcileConfigmapApply(foundConfigmap corev1.ConfigMap, newConfigmap corev1.ConfigMap) (bool, corev1.ConfigMap) {

	reconcileRequired := false

	if !reflect.DeepEqual(foundConfigmap.Data, newConfigmap.Data) {
		foundConfigmap.Data = newConfigmap.Data
		reconcileRequired = true
	}

	return reconcileRequired, foundConfigmap
}

func reconcilePodDisruptionBudgetApply(foundPodDisruptionBudget v1beta1.PodDisruptionBudget, newPodDisruptionBudget v1beta1.PodDisruptionBudget) (bool, v1beta1.PodDisruptionBudget) {

	reconcileRequired := false

	if !reflect.DeepEqual(foundPodDisruptionBudget.Spec.MaxUnavailable, newPodDisruptionBudget.Spec.MaxUnavailable) {
		foundPodDisruptionBudget.Spec.MaxUnavailable = newPodDisruptionBudget.Spec.MaxUnavailable
		reconcileRequired = true
	}

	if !reflect.DeepEqual(foundPodDisruptionBudget.Spec.MinAvailable, newPodDisruptionBudget.Spec.MinAvailable) {
		foundPodDisruptionBudget.Spec.MinAvailable = newPodDisruptionBudget.Spec.MinAvailable
		reconcileRequired = true
	}

	return reconcileRequired, foundPodDisruptionBudget
}

func reconcileStatefulsetApply(foundStatefulSet v1.StatefulSet, newStatefulSet v1.StatefulSet) (bool, v1.StatefulSet) {

	reconcileRequired := false

	if !reflect.DeepEqual(foundStatefulSet.Spec.Replicas, newStatefulSet.Spec.Replicas) {
		foundStatefulSet.Spec.Replicas = newStatefulSet.Spec.Replicas
		reconcileRequired = true
	}

	if !reflect.DeepEqual(foundStatefulSet.Labels, newStatefulSet.Labels) {
		foundStatefulSet.Labels = newStatefulSet.Labels
		reconcileRequired = true
	}

	if !reflect.DeepEqual(foundStatefulSet.Annotations, newStatefulSet.Annotations) {
		foundStatefulSet.Annotations = newStatefulSet.Annotations
		reconcileRequired = true
	}

	if !reflect.DeepEqual(foundStatefulSet.Spec.Template, newStatefulSet.Spec.Template) {
		foundStatefulSet.Spec.Template = newStatefulSet.Spec.Template
		reconcileRequired = true
	}

	return reconcileRequired, foundStatefulSet

}

func reconcileDeploymentApply(foundDeployment v1.Deployment, newDeployment v1.Deployment) (bool, v1.Deployment) {

	reconcileRequired := false

	if !reflect.DeepEqual(foundDeployment.Spec.Replicas, newDeployment.Spec.Replicas) {
		foundDeployment.Spec.Replicas = newDeployment.Spec.Replicas
		reconcileRequired = true
	}

	if !reflect.DeepEqual(foundDeployment.Labels, newDeployment.Labels) {
		foundDeployment.Labels = newDeployment.Labels
		reconcileRequired = true
	}

	if !reflect.DeepEqual(foundDeployment.Spec.Template, newDeployment.Spec.Template) {
		foundDeployment.Spec.Template = newDeployment.Spec.Template
		reconcileRequired = true
	}

	if !reflect.DeepEqual(foundDeployment.Annotations, newDeployment.Annotations) {
		foundDeployment.Annotations = newDeployment.Annotations
		reconcileRequired = true
	}

	return reconcileRequired, foundDeployment

}

func reconcileServiceApply(foundService corev1.Service, newService corev1.Service) (bool, corev1.Service) {

	reconcileRequired := false

	if !reflect.DeepEqual(foundService.Spec.Ports, newService.Spec.Ports) {
		foundService.Spec.Ports = newService.Spec.Ports
		reconcileRequired = true
	}

	if !reflect.DeepEqual(foundService.Spec.Type, newService.Spec.Type) {
		foundService.Spec.Type = newService.Spec.Type
		reconcileRequired = true
	}

	if !reflect.DeepEqual(foundService.Spec.ExternalTrafficPolicy, newService.Spec.ExternalTrafficPolicy) {
		foundService.Spec.Type = newService.Spec.Type
		reconcileRequired = true
	}

	if !reflect.DeepEqual(foundService.Labels, newService.Labels) {
		foundService.Labels = newService.Labels
		reconcileRequired = true
	}

	if !reflect.DeepEqual(foundService.Annotations, newService.Annotations) {
		foundService.Annotations = newService.Annotations
		reconcileRequired = true
	}

	return reconcileRequired, foundService
}
